package com.binance.assetservice.web.service.impl;

import com.binance.assetservice.data.mapper.TransactionMapper;
import com.binance.assetservice.data.models.TransactionRecord;
import com.binance.assetservice.vo.request.GetTranRequest;
import com.binance.assetservice.vo.request.GetTranRequest.TranType;
import com.binance.assetservice.web.service.ITranService;
import com.binance.master.error.BusinessException;
import com.binance.master.error.GeneralCode;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.Timestamp;

@Log4j2
@Service
public class TranServiceImpl implements ITranService {

    @Resource(name = "directWriteJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TransactionMapper transactionRecordDao;
    @Value("${transaction_record.useJdbcTemplate:false}")
    private Boolean useJdbcTemplate;

    private static final String insertSql = "insert into transaction_record (`type`, `date`, `description`) values (?, ?, ?)";

    @Override
    public Long insert(GetTranRequest item) {
        TransactionRecord transactionRecord = new TransactionRecord();
        TranType tranType = item.getType();
        Integer type = item.getTranType();
        if (tranType == null && type == null) {
            throw new BusinessException(GeneralCode.SYS_VALID);
        }
        if (type != null) {
            transactionRecord.setType(item.getTranType());
        } else {
            transactionRecord.setType(Integer.parseInt(tranType.getCode()));
        }
        transactionRecord.setDate(item.getTime());
        transactionRecord.setDescription(item.getDescription());
        if (useJdbcTemplate){
            log.info("ITranService.insert - JdbcTemplate");
            return this.doInsert(transactionRecord);
        }else {
            log.info("ITranService.insert - Sharding JDBC");
            transactionRecordDao.insert(transactionRecord);
            return transactionRecord.getId();
        }
    }

    /**
     * 直接使用jdbc插入，临时解决方案
     */
    private Long doInsert(TransactionRecord record){
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(
                con -> {
                    PreparedStatement ps = con.prepareStatement(insertSql, new String[]{ "type" ,"date", "description"});
                    ps.setInt(1, record.getType());
                    ps.setTimestamp(2, new Timestamp(record.getDate().getTime()));
                    ps.setString(3, record.getDescription());
                    return ps;
                }, keyHolder);

        return keyHolder.getKey().longValue();
    }
}
